/*
 * All rights Reserved, Designed By www.goldbeacon.cn
 * @Title:  demo-springboot
 * @Package com.chenzhengrong.demospringboot.queue
 * @author: chenzhengrong
 * @date:   2022/8/19 12:12
 * @version V1.0
 * Copyright © 2022 www.goldbeacon.cn. All Rights Reserved.
 * 注意：本内容仅限于湖南金烽信息科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.chenzhengrong.demospringboot.queue;

import com.chenzhengrong.demospringboot.domain.dto.OperateLogDTO;
import com.chenzhengrong.demospringboot.manager.OperatelogManager;
import com.chenzhengrong.demospringboot.mapper.OperatelogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenzhengrong
 * @title: OperateQueueConsumer
 * @description: OperateQueueConsumer
 * @date 2022/8/19 12:12
 */
@Component
public class OperateQueueConsumer implements Runnable {

    @Autowired
    private OperateQueue operateQueue;

    @Autowired
    private OperatelogManager operatelogManager;

    private boolean isActive = true;

    @PostConstruct
    public void init() {
        new Thread(this).start();
    }

    @PreDestroy
    public void close() {
        isActive = false;
    }

    @Override
    public void run() {
        //while (isActive) {
            exect();
        //}

    }

    private void exect() {

        List<OperateLogDTO> operateQueues = new ArrayList<>();

        int count = 0;
        while (count < 10) {
            OperateLogDTO poll = operateQueue.poll();
            if (poll == null) {
                break;
            }
            operateQueues.add(poll);
            operatelogManager.batchInsert(operateQueues);
            count++;
        }
        //isActive = false;
    }
}
